package mobidever.mygodutch.activity;

import mobidever.mygodutch.R;
import mobidever.mygodutch.R.layout;
import mobidever.mygodutch.activity.base.ActivityFrame;
import mobidever.mygodutch.adapter.AdapterAppGrid;
import mobidever.mygodutch.adapter.AdapterUser;
import mobidever.mygodutch.bussiness.BussinessUser;
import mobidever.mygodutch.controls.SliderItem;
import mobidever.mygodutch.controls.SliderMenu.OnSliderMenuClickListener;
import mobidever.mygodutch.model.ModelUser;
import mobidever.mygodutch.utility.RegexTool;
import android.R.integer;
import android.R.string;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ActivityUser extends ActivityFrame implements
		OnSliderMenuClickListener
{

	private ListView mUserList;
	private AdapterUser mAdapterUser;
	private BussinessUser mBussinessUser;
	private ModelUser mSelectModelUser;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		AppendMainBody(R.layout.user);
		initVariable();
		initView();
		initListener();
		bindData();
		createSlideMenu(R.array.SlideMenuUser);
	}

	private void initVariable()
	{
		mAdapterUser = new AdapterUser(null, this);
		mBussinessUser = new BussinessUser(this);
	}

	private void initView()
	{
		mUserList = (ListView) findViewById(R.id.lvUserList);
	}

	private void initListener()
	{
		registerForContextMenu(mUserList);
	}

	private void bindData()
	{
		mAdapterUser = new AdapterUser(null, this);
		// mAdapterUser.notifyDataSetChanged();
		mUserList.setAdapter(mAdapterUser);
	}

	private boolean deleteUser()
	{
		return mBussinessUser.HideUserByUserID((int) mSelectModelUser
				.getUserID());
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo)
	{
		AdapterContextMenuInfo _AdapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
		mSelectModelUser = (ModelUser) mUserList.getAdapter().getItem(
				_AdapterContextMenuInfo.position);

		menu.setHeaderIcon(R.drawable.user_small_icon);
		menu.setHeaderTitle(mSelectModelUser.getUserName());

		creatMenu(menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case 1:
			showUserAddOrEditDialog(mSelectModelUser);
			break;
		case 2:
			String _Title = getString(R.string.DialogMessageUserDelete,
					new Object[]
					{ mSelectModelUser.getUserName() });
			AlertDailogDeleteOrCanle(_Title, new OnEditOrDeleteListner());
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	public void onSliderMenuClick(View pView, SliderItem pSliderItem)
	{
		// showMsg(pSliderItem.getItemName());
		if (pSliderItem.getItemID() == 0)
		{
			showUserAddOrEditDialog(null);
			SlideMenuToggle();
		}
	}

	public void showUserAddOrEditDialog(ModelUser pModelUser)
	{
		String _Tittle;
		View _View = getMyLayoutInflater().inflate(R.layout.user_add_or_edit,
				null);
		EditText _EditText = (EditText) _View.findViewById(R.id.etUserName);

		if (pModelUser != null)
		{
			_EditText.setText(pModelUser.getUserName());
		}

		if (pModelUser != null)
		{
			_Tittle = getString(R.string.DialogTitleUser, new Object[]
			{ getString(R.string.TitleEdit) });
		} else
		{
			_Tittle = getString(R.string.DialogTitleUser, new Object[]
			{ getString(R.string.TitleAdd) });
		}

		AlertDialog.Builder _Builder = new AlertDialog.Builder(this);
		_Builder.setIcon(R.drawable.user_big_icon)
				.setTitle(_Tittle)
				.setView(_View)
				.setNeutralButton(getString(R.string.ButtonTextSave),
						new OnAddOrEditListener(pModelUser, _EditText, true))
				.setNegativeButton(getString(R.string.ButtonTextCancel),
						new OnAddOrEditListener(pModelUser, _EditText, false))
				.show();
	}

	private class OnAddOrEditListener implements
			DialogInterface.OnClickListener
	{
		private ModelUser mModelUser;
		private EditText etUserName;
		private boolean mIsSaveButton;

		public OnAddOrEditListener(ModelUser pModelUser, EditText pEditText,
				boolean pIsSaveButton)
		{
			mModelUser = pModelUser;
			etUserName = pEditText;
			mIsSaveButton = pIsSaveButton;
		}

		public void onClick(DialogInterface dialog, int which)
		{
			if (mIsSaveButton == false)
			{
				setAlertDialogIsClose(dialog, true);
				return;
			}

			if (mModelUser == null)
			{
				mModelUser = new ModelUser();
			}

			String _UserName = etUserName.getText().toString().trim();
			boolean _CheckResult = RegexTool.isChineseEnglishNum(_UserName);
			if (!_CheckResult)
			{
				showMsg(getString(R.string.CheckDataTextChineseEnglishNum,
						new Object[]
						{ getString(R.string.etUserNameHint) }));
				setAlertDialogIsClose(dialog, false);
				return;
			} else
			{
				setAlertDialogIsClose(dialog, true);
			}

			_CheckResult = mBussinessUser.IsExistUserByUserName(_UserName,
					(int) mModelUser.getUserID());

			if (_CheckResult)
			{
				showMsg(getString(R.string.CheckDataTextUserExist));
				setAlertDialogIsClose(dialog, false);
				return;
			} else
			{
				setAlertDialogIsClose(dialog, true);
			}

			mModelUser.setUserName(etUserName.getText().toString());
			boolean _Result = false;
			if (mModelUser.getUserID() == 0)
			{
				_Result = mBussinessUser.insertModelUser(mModelUser);

			} else
			{
				_Result = mBussinessUser.updateModelUserByID(mModelUser);
			}

			if (_Result)
			{
				bindData();
				showMsg(getString(R.string.TipsAddSucceed));
			} else
			{
				showMsg(getString(R.string.TipsAddFail));
			}
		}

	}

	private class OnEditOrDeleteListner implements
			DialogInterface.OnClickListener
	{

		public void onClick(DialogInterface dialog, int which)
		{
			boolean _Result = deleteUser();
			if (_Result)
			{
				bindData();
				showMsg(getString(R.string.TipsDeleteSeucceed));
			} else
			{
				showMsg(getString(R.string.TipsDeleteFail));
			}
		}
	}

}
